package com.service.impl;

import com.dao.MenusDao;
import com.dao.impl.MenusDaoImpl;
import com.pojo.Btn;
import com.pojo.Menus;
import com.service.MenusService;
import com.uilt.IndexInfo;
import com.uilt.LayuiTable;
import com.vo.TreeTableDto;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MenusServiceImpl implements MenusService {
    MenusDao menusDao = new MenusDaoImpl();

    @Override
    public List<Menus> getMenusAll(int uid, int type) {
        ResultSet menusAll = menusDao.getMenusAll(uid, type);
        List<Menus> menusList = new ArrayList<Menus>();
        try {
            while (menusAll.next()) {
                Menus menus = new Menus();
                menus.setId(menusAll.getInt("id"));
                menus.setmName(menusAll.getString("name"));
                menus.setPid(menusAll.getInt("parentid"));
                menus.setmType(menusAll.getInt("type"));
                menus.setHrefUrl(menusAll.getString("resUrl"));
                menus.setIcon(menusAll.getString("icon"));
                menusList.add(menus);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menusList;
    }

    @Override
    public List<Btn> getBtn() {
        ResultSet menusAll = menusDao.getBtn();
        List<Btn> menusList = new ArrayList<Btn>();
        try {
            while (menusAll.next()) {
                Btn btn = new Btn();
                btn.setId(menusAll.getInt("id"));
                btn.setBtnType(menusAll.getString("btntype"));
                menusList.add(btn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menusList;
    }

    @Override
    public List<Menus> getMenusAlls() {
        ResultSet menusAll = menusDao.getMenusAll();
        List<Menus> menusList = new ArrayList<Menus>();
        try {
            while (menusAll.next()) {
                Menus menus = new Menus();
                menus.setId(menusAll.getInt("id"));
                menus.setmName(menusAll.getString("name"));
                menus.setPid(menusAll.getInt("parentid"));
                menus.setmType(menusAll.getInt("type"));
                menus.setResKey(menusAll.getString("resKey"));
                menus.setHrefUrl(menusAll.getString("resUrl"));
                menus.setIcon(menusAll.getString("icon"));
                menusList.add(menus);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menusList;
    }


    @Override
    public List<Menus> getMenusBtn(int uid, int pid, int type) {
        ResultSet menusAll = menusDao.getMenusBtn(uid, pid, type);
        List<Menus> menusList = new ArrayList<Menus>();
        try {
            while (menusAll.next()) {
                Menus menus = new Menus();
                menus.setId(menusAll.getInt("id"));
                menus.setmName(menusAll.getString("name"));
                menus.setPid(menusAll.getInt("parentid"));
                menus.setResKey(menusAll.getString("reskey"));
                menus.setmType(menusAll.getInt("type"));
                menus.setHrefUrl(menusAll.getString("resUrl"));
                menus.setIcon(menusAll.getString("icon"));
                menusList.add(menus);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menusList;
    }

    @Override
    public List<IndexInfo> getLayMenusAll(int uid, int type) {
        //绑定了二级菜单的目录
        List<IndexInfo> infoListAll = new ArrayList<IndexInfo>();
        //查询一级目录
        List<Menus> menusAll1 = getMenusAll(uid, 0);
        for (Menus menus : menusAll1) {
            IndexInfo indexInfo1 = new IndexInfo();
            indexInfo1.setTitle(menus.getmName());
            indexInfo1.setHref("");
            indexInfo1.setIcon(menus.getIcon());
            indexInfo1.setImage("");
            //查询二级菜单
            List<Menus> menusAll2 = getMenusBtn(uid, menus.getId(), 1);
            //存放二级菜单
            List<IndexInfo> infoList2 = new ArrayList<IndexInfo>();
            for (Menus menus1 : menusAll2) {
                IndexInfo indexInfo2 = new IndexInfo();
                indexInfo2.setTitle(menus1.getmName());
                indexInfo2.setHref(menus1.getHrefUrl() + "&&pid=" + menus1.getId());
                indexInfo2.setIcon(menus1.getIcon());
                indexInfo2.setImage("");
                indexInfo2.setChild(null);
                infoList2.add(indexInfo2);
            }
            indexInfo1.setChild(infoList2);
            infoListAll.add(indexInfo1);
        }
        return infoListAll;
    }

    @Override
    public LayuiTable<TreeTableDto> getMenusAll() {
        List<Menus> menusAll = getMenusAlls();
        List<TreeTableDto> treeTableDtos = new ArrayList<TreeTableDto>();
        for (Menus menus : menusAll) {
            TreeTableDto treeTableDto = new TreeTableDto();
            treeTableDto.setAuthority(menus.getResKey());
            treeTableDto.setAuthorityId(menus.getId());
            treeTableDto.setAuthorityName(menus.getmName());
            treeTableDto.setChecked(true);
            treeTableDto.setOpen(true);
            if (menus.getPid() == 0) {
                treeTableDto.setParentId(menus.getPid() - 1);
            } else {
                treeTableDto.setParentId(menus.getPid());
            }
            treeTableDto.setIsMenu(menus.getmType());
            treeTableDtos.add(treeTableDto);
        }

        LayuiTable<TreeTableDto> layuiTable = new LayuiTable<TreeTableDto>();
        layuiTable.setData(treeTableDtos);
        layuiTable.setCode(0);
        layuiTable.setMsg("");
        layuiTable.setCount(100);

        return layuiTable;
    }
}


